package com.qumitech.visualnovel.service.oldgame.imp;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.game.GameConfigVo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.game.GameConfigCopyDTO;
import com.qumitech.visualnovel.service.oldgame.OldGameConfigService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: yilu
 * @Date: 2018/11/23 11:53
 * @Description:
 */

@Service
public class OldGameConfigServiceImpl implements OldGameConfigService {

    private final String GAME_CONFIG_URL = "/game";
    private final String GAME_BASE_INFO_URL = "/game/baseinfo";

    @Override
    public Map<String, Object> getGames(String gameId, String gameName, String appChannel, String appVersion, Integer offset, Integer limit) {
        String url = UrlConstant.SERVER_URL + GAME_CONFIG_URL;
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("offset", offset.toString());
        paramMap.put("limit", limit.toString());
        if (gameId != null && !"".equals(gameId)) {
            paramMap.put("game_type", gameId);
        }
        if (gameName != null && !"".equals(gameName)) {
            paramMap.put("game_name", gameName);
        }
        if (appChannel != null) {
            paramMap.put("app_channel", appChannel);
        }
        if (appVersion != null) {
            paramMap.put("app_version", appVersion);
        }
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        if (rspMap.get("games") == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rspMap;
    }

    @Override
    public void updateGame(GameConfigVo gameConfigVo) {
        String url = UrlConstant.SERVER_URL + GAME_CONFIG_URL;
        String req = JSONObject.toJSONString(gameConfigVo);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void addGame(GameConfigVo gameConfigVo) {
        String url = UrlConstant.SERVER_URL + GAME_CONFIG_URL;
        String req = JSONObject.toJSONString(gameConfigVo);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void removeGame(String gameType, String appChannel, String appVersion) {
        appChannel = appChannel.replace(" ", "+");
        String url = UrlConstant.SERVER_URL + GAME_CONFIG_URL + "?app_version=" + appVersion + "&app_channel=" + appChannel;
        if (gameType != null) {
            url = url + "&game_type=" + gameType;
        }
        Map<String, String> headMap = new HashMap<>();

        String body = HttpInvokeUtil.httpDelete(url, headMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void copyGame(GameConfigCopyDTO configCopyDTO) {
        String url = UrlConstant.SERVER_URL + GAME_CONFIG_URL + "/copy";
        String req = JSONObject.toJSONString(configCopyDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public Map<String, Object> getGameIndexInfo(String appChannel, String appVersion) {
        String url = UrlConstant.SERVER_URL + GAME_CONFIG_URL + "/info";
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("app_channel", appChannel);
        paramMap.put("app_version", appVersion);
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public Map<String, Object> getGameBaseInfo() {
        String url = UrlConstant.SERVER_URL + GAME_BASE_INFO_URL;
        Map<String, String> paramMap = new HashMap<>();
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }
}
